home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / bathints.zip / BH_10.DOC < prev    next >
Encoding:
Text File  |  1988-10-16  |  14.0 KB  |  363 lines

  1.  
  2.                                   BAT-HINT # 10
  3.  
  4. **************************************************************************
  5.  
  6. from the BATHINTS library... part of the BATPOWER CONFERENCE from:
  7.  
  8.                          THE PAINFRAME OPUS/FIDO 261/1004 
  9.  
  10.                         Baltimore, Maryland 1-301-488-7461
  11.  
  12. **************************************************************************
  13.  
  14.                                The IF subcommand
  15.  
  16. The IF subcommand is not unique to batch file processing in DOS... the
  17. command may be issued from either a batch file or from the command line,
  18. though it finds its greatest use from within the batch file.  Through the
  19. use of IF a user may check for several things:
  20.  
  21.     1.  whether a file exists
  22.     2.  whether a specific errorlevel has been returned from the keyboard
  23.         or from a program
  24.     3.  whether a variable exists
  25.     4.  the value of a variable or string
  26.  
  27. The general syntax of the IF command is:
  28.  
  29.     if [not] {true statement} {perform command}
  30.  
  31. where [not] is optional, {true statement} is a legal statement to test with
  32. the IF command and {perform command} is any executable command.
  33.  
  34. Errorlevel Testing
  35. ------------------
  36.  
  37. Many of you have no doubt already used the IF command to test for
  38. errorlevels, so lets start with a simple example of an errorlevel test:
  39.  
  40.     sample1.bat
  41.     -----------
  42.  
  43.     echo off
  44.     cls
  45.     :start
  46.     echo Press a key... (you must press the right one!)
  47.     key
  48.     if errorlevel 65 goto correct
  49.     echo Sorry, that was the wrong key...
  50.     goto start
  51.     :correct
  52.     echo You pressed uppercase A
  53.     echo That was the letter I was waiting for!
  54.  
  55. This example uses the KEY.COM utility available from BATPOWER.ARC, a
  56. utility that pauses the execution of a batch file and waits for a single
  57. key to be pressed at the keyboard... and then returns an errorlevel code
  58. equal to the scan code of the key pressed (all keys and key combinations
  59. have an associated scan code used by DOS; for details on the specific scan
  60. codes see BATHINT ? ?... bh_?.doc; for details on the use of KEY.COM see
  61. the doc file KEY.DOC packaged with the program in BATPOWER.ARC).  In this
  62. example, the IF command is waiting for the uppercase (capital) A to be
  63. pressed... its scan code is 65.  Lets examine the command in more detail:
  64.  
  65.     if errorlevel 65 goto correct
  66.  
  67. We can divide the command statement into three parts according to the
  68. syntax already given.  First, IF, well thats what we are talking about. 
  69. Second, errorlevel 65, that is the {true statement} that we are testing...
  70. is the errorlevel 65, or more precisely, is the errorlevel equal to or
  71. greater than 65?  Thats the way testing for errorlevels works.  If a
  72. statement is made like:
  73.  
  74.     if errorlevel 65 {perform command}
  75.  
  76. then the IF command tests whether the errorlevel returned is equal to or
  77. greater than the specified number.  In sample1.bat we could narrow down the
  78. specificity of the test with a statement like:
  79.  
  80.     if errorlevel 65 if not errorlevel 66 {perform command}
  81.  
  82. AhAh!  A nested IF command.  Examine closely what this statement says: If
  83. the errorlevel is 65 or greater AND if the errorlevel is NOT 66 or greater
  84. then {perform command}... so the errorlevel must be equal to 65 (or more)
  85. but not equal to 66 (or more)... thus the errorlevel MUST be 65 for the
  86. statement to be true.  If the statement is true (if an A was pressed) then
  87. {perform command}, which in sample1.bat is GOTO CORRECT, is executed.  If
  88. it is false (if the errorlevel is less than 65) then {perform command} is
  89. not executed and the batch file ignores {perform command} and jumps to the
  90. next line ( the line below).  So with "if errorlevel XX if not errorlevel
  91. XX+1" we may single out a specific errorlevel for testing.  Examining the
  92. third part of the statement "goto correct", we see the command to be
  93. executed IF THE STATEMENT IS TRUE.  GOTO is batch file specific command in
  94. DOS which instructs DOS to jump to a specified label.  In our example, the
  95. label is "correct" (though it could be anything, so long as that label
  96. exists).  Notice that further down in the batch file is the following line:
  97.  
  98.     :correct
  99.  
  100. and notice also that when specifying a label with the GOTO command it is
  101. not necessary to include the colon (:) before the label (but the label must
  102. have a colon where it actually appears as a directive in the batch file).  
  103. So if everything is true (you actually did press the letter A), sample1.bat
  104. performs the "GOTO CORRECT" command and searches the batch file for the
  105. label :correct then continues executing each line, one at a time, from the
  106. label :correct.  In our example, two echo commands are executed indicating
  107. that that was the right key to press... and the batch file ends.  If the
  108. statement was FALSE (you did not press A; the errorlevel was less than and
  109. not equal to 65) then the batch file ignores the "goto correct" command and
  110. skips to the next line, which echos that you pressed the wrong key and then
  111. performs the "goto start" command, which jumps back to the :start label and
  112. asks you to press a key again (the letter A!).
  113.  
  114. So great eh?  What can I do with it?  Well there are many applications,
  115. only one of which is a small menu of possible selections.  As a last
  116. example, consider the following scenerio:  You have three programs from
  117. which to choose and you have written a batch file which starts each
  118. application by its name.  The applications are:  Yorgi (a fanatstic game of
  119. fun and skill?!), RipWrite (a great word processor) and Snarf (a utility
  120. program that you cannot live without).  Lets assume that each batch file
  121. (yorgi.bat, ripwrite.bat and snarf.bat) are available through the normal
  122. DOS command line by virtue of the fact that they are in the c:\bat
  123. subdirectory that is named in your path command... so that they be found
  124. when called upon).  Rather than type the name of the batch file which
  125. starts these great programs, you have created a simple menu from which with
  126. a single keystroke you may select any one of them.  That batch file may be
  127. written like this:
  128.  
  129.     menu.bat
  130.     --------
  131.  
  132.     echo off
  133.     cls
  134.     echo Main Menu:
  135.     echo      1.  Yorgi
  136.     echo      2.  RipWrite
  137.     echo      3.  Snarf
  138.     echo Press a number according to your selection...
  139.     key
  140.     if errorlevel 51 if not errorlevel 52 goto select3
  141.     if errorlevel 50 if not errorlevel 51 goto select2
  142.     if errorlevel 49 if not errorlevel 50 goto select1
  143.     goto sorry
  144.     :select1
  145.     yorgi
  146.     :select2
  147.     ripwrite
  148.     :select3
  149.     snarf
  150.     :sorry
  151.     echo You must press a number between 1 and 3
  152.     pause
  153.     goto start
  154.     cls
  155.  
  156. Notice that the IF ERRORLEVEL statements are numbered backwards from the
  157. scan code for number 3 (51) to the scan code for number 1 (49).  Following
  158. the logic given for sample1.bat, you should be able to see that you must
  159. start from the highest number down (if you have questions just ask!!!).
  160.  
  161. Testing for the Presence of a File
  162. ----------------------------------
  163.  
  164. IF can test for the existance of a file.  Examine the following batch
  165. file:
  166.  
  167.     sample2.bat
  168.     -----------
  169.  
  170.     echo off
  171.     cls
  172.     if exist myword.doc echo myword.doc exists!
  173.     cls
  174.  
  175. Simple enough... sample2.bat tests for the existance of a file called
  176. "myword.doc" and if the statement is true (that is, if myword.doc does
  177. exist), it executes the command "echo myword.doc exists!".  If the file is
  178. not found the remainder of the command line is ignored and the batch file
  179. continues processing at the next line, which in this example clears the
  180. screen.  Note that the "if exist" command only recognizes files in the
  181. current directory.  To test for a file located elsewhere you should log
  182. onto the drive and into the subdirectory where the file is presumed to
  183. exist.  For example, if the file was located on drive B in a subirectory
  184. called "document", you would need to change sample2.bat to read:
  185.  
  186.  
  187.     echo off
  188.     cls
  189.     b:
  190.     cd\document
  191.     if exist b:\document\myword.doc echo myword.doc exists!
  192.  
  193. Occasionally, the test for a specific file (as in sample2.bat) may be
  194. useful, but more commonly the filename is substituted for a REPLACEABLE
  195. PARAMETER that the user can specify when the batch file is initially called
  196. into action.  Our example could be made more general by making the changes
  197. shown below:
  198.  
  199.     sample3.bat
  200.     -----------
  201.  
  202.     echo off
  203.     cls
  204.     if exist %1 echo %1 exists!
  205.     cls
  206.  
  207. In this example, the %1 parameter would be entered at the command line when
  208. the batch file is started.  For example, to test for the existance of a
  209. file called "another.doc" the user would start the batch file with the
  210. following command:
  211.  
  212.     sample3 another.doc
  213.  
  214. The %0 parameter is always the name of the batch file (sample3 in this
  215. case) and "another.doc" is the %1 parameter.  When the "if exist" line is
  216. executed, the term "another.doc" will be substituted for %1.
  217.  
  218. More often than not, it is more useful to utilize a "goto" statement after
  219. the IF command.  In sample2.bat and sample3.bat the "echo filename.ext!"
  220. message would only be flashed on the screen momentarily if the statement
  221. was true since the next and last line in these batch files issues the CLS
  222. (clear screen) command.  A more refined way to write these batch files is
  223. shown below:
  224.  
  225.     sample4.bat
  226.     -----------
  227.  
  228.     echo off
  229.     cls
  230.     if exist %1 goto foundit
  231.     echo Sorry, %1 does not exist.
  232.     pause
  233.     goto fini
  234.     :foundit
  235.     echo %1 exists!
  236.     pause
  237.     :fini
  238.     cls
  239.  
  240. In this example, if %1 exists batch file processing jumps to the "foundit"
  241. label and echos that fact... then pauses for a keystroke before clearing
  242. the screen and terminating.  If %1 does not exist it echos "Sorry...",
  243. waits for a keystroke and jumps to the "fini" label then clears the screen
  244. and terminates.
  245.  
  246. At first glance, the usefullness of checking for the existance of a file
  247. doesn't seem to have much use... but consider this example:
  248.  
  249. You normally store some of your programs as compressed archives to save on
  250. disk space.  One such program, a game called "WASTER" is archived into a
  251. single file called "waster.arc" on drive C in the subdirectory GAMES.  You
  252. have a batch file that starts the program by unarchiving the program onto a
  253. large RAM disk (say drive H), then runs the game (waster.exe) from the RAM
  254. drive.  Here is a batch file that might do such processing... and also
  255. check to see if waster.arc is already unarchived on drive H... and if so,
  256. does not try to unarc it again, but rather justs runs the program from the
  257. RAM drive.  This example uses the program PKXARC.EXE from PKWARE to do the
  258. unarchiving:
  259.  
  260.     waster.bat
  261.     ----------
  262.  
  263.     echo off
  264.     cls
  265.     if not exist h:\waster.exe pkxarc c:\games\waster.arc h:\
  266.     h:
  267.     waster
  268.  
  269. Equivalence Testing
  270. -------------------
  271.  
  272. The correct syntax for testing equivalence is:
  273.  
  274.     if stringA==stringB {perform command}
  275.  
  276. where stringA and stringB are ASCII characters and may be substituted for
  277. replaceable parameters and/or environmental variables.  Very often one may
  278. wish to include a parameter after the name of a batch file so as to direct
  279. some special function.  For example, password.bat tests for the presence of
  280. a password given as the %1 replaceable parameter.  In this example, the
  281. password is "eyesonly":
  282.  
  283.     password.bat
  284.     ------------
  285.  
  286. echo off
  287. cls
  288. if %1==eyesonly goto correct
  289. echo Your password was incorrect.
  290. goto end
  291. :correct
  292. {perform some commands here}
  293. :end
  294.  
  295. In order that the command that you would place on line 7 to be invoked, you
  296. would have to enter the following statement at the command line:
  297.  
  298.     password eyesonly
  299.  
  300. Nothing else would work.  If the incorrect parameter is given, or if no
  301. parameter was given, the IF statement would not be true and the file would
  302. display "Your password was incorrect".  Of course, such a batch file would
  303. not provide any substantial level of security, since a user would only need
  304. to view the batch file with an editor or with the type command to see what
  305. the correct password should be.  Note that the password must match exactly. 
  306. If the command was given as:
  307.  
  308.     password EYESONLY
  309.     or
  310.     password eysOnly
  311.  
  312. or any other variation of upper and lower case letters, the IF statement
  313. would be false.
  314.  
  315. Often it is of benefit to test for the absence of a parameter, especially
  316. when you may have forgotten the correct syntax for a complicated batch file
  317. written some time ago.  Note the following statement:
  318.  
  319.     if "%1"=="" {perform command}
  320.  
  321. This statement will be true when the %1 parameter is a NULL string... the
  322. %1 parameter was not given on the command line which started the batch
  323. file.  Note that the NULL string is a real string... it is just a string
  324. with a size of zero.  If the quotation marks were left out:
  325.  
  326.     if %1=="" {perform command}
  327.  
  328. a syntax error message would be generated.  The quotation marks can just as
  329. easily be replaced with dollar signs, exclamation marks... almost anything. 
  330. As an example several statements are shown below.  If no parameter is given
  331. they are all true, if a parameter was given, they are all false, and one
  332. works just as well as any of the others:
  333.  
  334.     if "%1"=="" {perform command}
  335.     if $%1$==$$ {perform command}
  336.     if !%1!==!! {perform command}
  337.     if x%1==x {perform command}
  338.     if %1x==x {perform command}
  339.     if .%1==. {perform command}
  340.  
  341. Use whichever style you prefer.
  342.  
  343. Tests for equivalence may also employ environmental variables (see BATHINT?
  344. for a discussion of environmental variables).  As always, the environmental
  345. variable must be enclosed within quotation marks for its value to be
  346. correctly substituted.  For example, if the following command had been
  347. issued:
  348.  
  349.     set name=Bimbo
  350.  
  351. so that the variable "name" contained the value "Bimbo", the value of the
  352. variable could be tested with the following statements:
  353.  
  354.     if %name%==Bimbo goto correct
  355.     echo The name was not Bimbo.
  356.     goto end
  357.     :correct
  358.     echo The name was Bimbo.
  359.     :end
  360.  
  361. *********************************************************** David Creasey
  362.  
  363.